import { Modal, Radio } from 'antd'
import React from 'react'
import ScreenShareImage from 'src/components/screen_share_image'
import TRTCCloud from 'src/lib/srtc-electron-sdk'

export default function getShareWindow(trtc: TRTCCloud) {
  const shareList = trtc.getScreenShareList()
  console.log(shareList)

  let shareWinItem
  return new Promise((resolve, reject) => {
    Modal.confirm({
      title: '选择分享窗口',
      icon: null,
      width: '80%',
      content: (
        <div>
          <Radio.Group
            defaultValue=""
            onChange={(e) => {
              shareWinItem = shareList.find((d) => d.sourceId === e.target.value) || shareList[0]
            }}
          >
            {shareList.map((win) => (
              <Radio.Button value={win.sourceId} key={win.sourceId} style={{ height: 'auto', margin: '6px' }}>
                <div style={{ width: '100%', height: '100%', display: 'flex', flexDirection: 'column', justifyContent: 'center', alignItems: 'center' }}>
                  <span>
                    {win.type !== 0 ? '桌面-' : '窗口-'}
                    {win.sourceName.substr(0, 8)}
                  </span>
                  <ScreenShareImage buffer={win.thumbBGRA.buffer} width={win.thumbBGRA.width} height={win.thumbBGRA.height} />
                </div>
              </Radio.Button>
            ))}
          </Radio.Group>
        </div>
      ),
      onOk() {
        resolve(shareWinItem)
      },
      onCancel() {
        resolve(null)
      }
    })
  })
}
